Skip to content

fix(portfolio): 空头持仓的未实现盈亏百分比按数量方向计算 - #417

Closed
xxstar-01 wants to merge 1 commit into
helsome:mainfrom
xxstar-01:fix/short-position-pnl-percent
Closed

xxstar-01 wants to merge 1 commit into
helsome:mainfrom
xxstar-01:fix/short-position-pnl-percent

Conversation

@xxstar-01

Copy link
Copy Markdown
Contributor

改了什么

packages/longbridge-tools/src/normalizer.ts:

  • computeUnrealizedPnLPercent 增加可选的第三个参数 quantity;传入负数时按空头方向取反,使百分比与 computeUnrealizedPnL(cost, market, quantity) 的金额同号。
  • normalizeHolding 调用处传入 quantity。
  • 不传 quantity(原有两参数调用)、quantity = 0 或非有限数时,保持原纯价格口径。

packages/longbridge-tools/src/normalizer.test.ts:新增两条用例,覆盖空头正反两向、金额与百分比同号断言,以及正数 / 零 / 非有限 / 缺失数量的兼容性。

为什么要改

Issue #397 的复现:quantity=-10, cost=100, market=120 时金额为 -$200,百分比却是 +20%;market=80 时金额 +$200、百分比 -20%。原因是百分比只看价格涨跌,不看数量方向,两个数字互相矛盾。金额、市值与权重语义不变,也没有反转 provider 已上报的 ratio。

关联 Issue

Closes #397

测试报告

环境:Bun 1.4.4 / Windows 11 家庭版 10.0.26200 / AMD64;基线 f5822f0cb27b1f5843fe3ed5cc2f3f5baecc75de。

bun test packages/longbridge-tools/src/normalizer.test.ts
→ 16 pass / 1 skip / 0 fail
→ 17 tests, 194 expect() calls
→ 改动前同一文件:14 pass / 1 skip / 0 fail

bun test --isolate
→ 2231 pass / 8 skip / 0 fail
→ 231 files, 11881 expect() calls

bun run typecheck
→ @finagent/core / i18n / shared / ui / electron 全部 Exited with code 0

关于 8 项 skip:均为既有跳过项(真实 CLI fixture 需要私有账户数据、phase-2 旧契约用例、Windows 符号链接需开发者模式),与本次改动无关,改动前后一致。

  • 已提供实际测试命令与 pass/fail 结果
  • 已说明测试环境
  • 核心改动有对应 focused test 覆盖,并跑了全仓与 typecheck

UI 截图

  • 本 PR 无可见 UI 变化

百分比数值会变(空头从 +20% 变 -20%),但这是数据正确性修复,不涉及组件结构、布局或样式;HoldingRow 显示层未改动。

Scope / 后续

只改百分比的符号口径。未改动金额、市值、权重,未引入 provider 已上报 ratio 的反转,未处理 #356–#360 的解析范围。

computeUnrealizedPnLPercent 增加可选 quantity 参数,负数时取反,使百分比与
computeUnrealizedPnL 的金额同号;两参数旧调用、零值与非有限数保持原价格口径。
normalizeHolding 传入 quantity,并补空头正反两向与兼容性用例。

Closes helsome#397

@helsome helsome left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本 PR 与已合入的 #402 都修复 #397 的同一空头盈亏百分比问题。#402 已包含 normalizer 与 portfolio.summary 端到端回归、零值/分数仓位兼容和全量 Linux 验证,因此当前改动不再需要重复合入。感谢贡献;建议关闭本 PR,后续如发现 #402 未覆盖的边界,请单独提交回归。

@helsome helsome closed this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(portfolio): 空头持仓的盈亏金额与百分比方向相反

2 participants